package com.yjf.indoormap.ui.activity;

import android.os.Bundle;

import com.yjf.indoormap.db.GreenDaoUtils;
import com.yjf.indoormap.entity.Shop;
import com.yjf.indoormap.greenDao.ShopDao;
import com.yjf.indoormap.utils.DataImportUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yjf on 2017/4/2.
 * 闪屏
 */

public class SplashActivity extends BaseActivity {


    @Override
    protected int getLayoutResId() {
        return 0;
    }


    @Override
    protected void initView() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                importDataFromXls();
                emitter.onNext(0);
                emitter.onComplete();
            }
        })
                .delay(2, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object aLong) throws Exception {
                        toMain();
                    }
                });
    }


    private void toMain() {
        startActivity(LoginActivity.class);
        finish();
    }

    private void importDataFromXls() {
        ShopDao shopDao = GreenDaoUtils.getSingleTon().getSession().getShopDao();
        if (!shopDao.loadAll().isEmpty()){
            return;
        }
        List<Shop> shopList = new DataImportUtils().getXlsData(this);
        shopDao.insertOrReplaceInTx(shopList);
    }


}
